package cn.xiqi.cms.action;

import java.util.HashMap;
import java.util.Map;

import cn.xiqi.cms.oper.AccountOper;
import cn.xiqi.cms.oper.UserOper;
import net.sf.json.JSON;
import net.sf.json.JSONObject;

public class UserAction extends BaseAction {

	private static final long serialVersionUID = 266378687020963497L;
	private Map<String, Object> map;
	private JSON json;
	private String account;
	private String password;
	private String name;
	private Integer sex;
	private Integer depart;
	private Integer level;
	private Integer role;
	private Integer page;
	private Integer size;
	private Integer userid;
	private Integer stat;
	
	public String getAll(){
		UserOper oper = new UserOper();
		int page = Integer.valueOf(super.getParameter("page"));
		int size = Integer.valueOf(super.getParameter("size"));
		
		json = new JSONObject();		
		map = oper.getAll(page, size);
		json = JSONObject.fromObject(map);
		return SUCCESS; 
	}
	
	public String delete(){
		int userid = Integer.valueOf(super.getParameter("userid"));
		int page = Integer.valueOf(super.getParameter("page"));
		int size = Integer.valueOf(super.getParameter("size"));
		UserOper oper = new UserOper();
		boolean issuccess = oper.delete(userid);
		
		map = new HashMap<>();
		if(issuccess){
			map.put("success", true);
			map.put("data", oper.getAll(page, size));
		}else{
			map.put("success", false);
		}
		json = JSONObject.fromObject(map);
		return SUCCESS;
	}
	
	public String add(){
		UserOper userOper = new UserOper();
		boolean isExist;
		AccountOper accountOper = new AccountOper();
		
		map = new HashMap<>();
		isExist = accountOper.isExist(account);
		if(isExist){
			map.put("success", false);
		}else{
			try{
				int userid = userOper.addUser(name, sex, depart, level, role);
				accountOper.register(account, password, userid);
				int count = userOper.getCount("all");
				page = (count-1)/size + 1;
				map.put("success", true);
				map.put("data", userOper.getAll(page, size));
			}
			catch(Exception e){
				map.put("success", false);
			}
		}
		json = JSONObject.fromObject(map);
		return SUCCESS;
	}
	
	public String getByID(){
		int userid = Integer.valueOf(super.getParameter("userid"));
		UserOper oper = new UserOper();
		
		try{
			map = oper.getByID(userid);
			map.put("success", true);
		}
		catch(Exception e){
			map.put("success", false);
		}
		json = JSONObject.fromObject(map);
		return SUCCESS;
	}
	
	public String edit(){
		UserOper userOper = new UserOper();
		map = new HashMap<>();
		try{
			userOper.update(userid, name, depart, level, role, sex, stat);
			map.put("success", true);
			map.put("data", userOper.getAll(page, size));
		}
		catch(Exception e){
			map.put("success", false);
		}
		json = JSONObject.fromObject(map);
		return SUCCESS;
	}

	public JSON getJson() {
		return json;
	}

	public void setJson(JSON json) {
		this.json = json;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getSex() {
		return sex;
	}

	public void setSex(Integer sex) {
		this.sex = sex;
	}

	public Integer getDepart() {
		return depart;
	}

	public void setDepart(Integer depart) {
		this.depart = depart;
	}

	public Integer getLevel() {
		return level;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}

	public Integer getRole() {
		return role;
	}

	public void setRole(Integer role) {
		this.role = role;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getSize() {
		return size;
	}

	public void setSize(Integer size) {
		this.size = size;
	}

	public Integer getUserid() {
		return userid;
	}

	public void setUserid(Integer userid) {
		this.userid = userid;
	}

	public Integer getStat() {
		return stat;
	}

	public void setStat(Integer stat) {
		this.stat = stat;
	}

}
